package com.wh.wisdomsite.xbox.server.util;

import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.utils.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

	public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String FORMAT_HH_MM_SS = "HH:mm:ss";
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public static final Long getLongTime(String strDate){
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date date;
		Long time=0L;
		try {
			date = df.parse(strDate);
			time=date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;
	}

	public static String dateStr8(Date date) {
		// 招商贷需求变更 短信时间由12小时改为24小时制 高才 2012/08/12 update start
		// SimpleDateFormat format = new
		// SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");2003-07-14T09:30:47
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddTOHH:mm:ss");
		// 高才 2012/08/12 update end
		String str = format.format(date);
		return str;
	}
	
	public static String dateStr9(Date date) {
		// 招商贷需求变更 短信时间由12小时改为24小时制 高才 2012/08/12 update start
		// SimpleDateFormat format = new
		// SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");2003-07-14T09:30:47
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		// 高才 2012/08/12 update end
		String str = format.format(date);
		return str;
	}
	
	public static String dateStr5(Date date) {
		// 招商贷需求变更 短信时间由12小时改为24小时制 高才 2012/08/12 update start
		// SimpleDateFormat format = new
		// SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
		// 高才 2012/08/12 update end
		String str = format.format(date);
		return str;
	}

	// 将字符串转为时间戳
	public static String getTime(String user_time) {

		String re_time = null;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");

		Date d;

		try {

			d = sdf.parse(user_time);

			long l = d.getTime();

			String str = String.valueOf(l);

			re_time = str.substring(0, 10);

		} catch (ParseException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}

		return re_time;

	}

	/**
	 * 将秒转换成时间
	 * 
	 * @param times
	 * @return
	 */
	public static Date getDate(String times) {
		long time = Long.parseLong(times);
		return new Date(time * 1000);
	}

	public static long getTime(Date date) {
		return date.getTime() / 1000;
	}

	public static int getDay(Date d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * s - 表示 "yyyy-mm-dd" 形式的日期的 String 对象
	 * 
	 * @param
	 * @return
	 */
	public static Date valueOf(String s) {
		final int YEAR_LENGTH = 4;
		final int MONTH_LENGTH = 2;
		final int DAY_LENGTH = 2;
		final int MAX_MONTH = 12;
		final int MAX_DAY = 31;
		int firstDash;
		int secondDash;
		int threeDash = 0;
		int fourDash = 0;
		Date d = null;

		if (s == null) {
			throw new IllegalArgumentException();
		}

		firstDash = s.indexOf('-');
		secondDash = s.indexOf('-', firstDash + 1);
		if (s.contains(":")) {
			threeDash = s.indexOf(':');
			fourDash = s.indexOf(':', threeDash + 1);
		}
		if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) {
			String yyyy = s.substring(0, firstDash);
			String mm = s.substring(firstDash + 1, secondDash);
			String dd = "";
			String hh = "";
			String MM = "";
			String ss = "";
			if (s.contains(":")) {
				dd = s.substring(secondDash + 1, threeDash - 3);
				hh = s.substring(threeDash - 2, threeDash);
				MM = s.substring(threeDash + 1, fourDash);
				ss = s.substring(fourDash + 1);
			} else {
				dd = s.substring(secondDash + 1);
			}
			if (yyyy.length() == YEAR_LENGTH && mm.length() == MONTH_LENGTH && dd.length() == DAY_LENGTH) {
				int year = Integer.parseInt(yyyy);
				int month = Integer.parseInt(mm);
				int day = Integer.parseInt(dd);
				int hour = 0;
				int minute = 0;
				int second = 0;
				if (s.contains(":")) {
					hour = Integer.parseInt(hh);
					minute = Integer.parseInt(MM);
					second = Integer.parseInt(ss);
				}
				if (month >= 1 && month <= MAX_MONTH) {
					int maxDays = MAX_DAY;
					switch (month) {
					// February determine if a leap year or not
					case 2:
						if ((year % 4 == 0 && !(year % 100 == 0)) || (year % 400 == 0)) {
							maxDays = MAX_DAY - 2; // leap year so 29 days in
													// February
						} else {
							maxDays = MAX_DAY - 3; // not a leap year so 28 days
													// in February
						}
						break;
					// April, June, Sept, Nov 30 day months
					case 4:
					case 6:
					case 9:
					case 11:
						maxDays = MAX_DAY - 1;
						break;
					}
					if (day >= 1 && day <= maxDays) {
						Calendar cal = Calendar.getInstance();
						cal.set(year, month - 1, day, hour, minute, second);
						cal.set(Calendar.MILLISECOND, 0);
						d = cal.getTime();
					}
				}
			}
		}
		if (d == null) {
			throw new IllegalArgumentException();
		}
		return d;
	}

	/**
	 * @author lijie
	 * @param Begin
	 * @param end
	 *            传入开始时间 和 结束时间 格式如：2012-09-07
	 * @return 返回Map 获取相隔多少年 get("YEAR")及为俩个时间年只差，月 天，类推 Key ： YEAR MONTH DAY
	 *         如果开始时间 晚于 结束时间 return null；
	 */

	public static Map getApartTime(String Begin, String end) {
		String[] temp = Begin.split("-");
		String[] temp2 = end.split("-");
		if (temp.length > 1 && temp2.length > 1) {
			Calendar ends = Calendar.getInstance();
			Calendar begin = Calendar.getInstance();

			begin.set(NumberUtils.getInt(temp[0]), NumberUtils.getInt(temp[1]), NumberUtils.getInt(temp[2]));
			ends.set(NumberUtils.getInt(temp2[0]), NumberUtils.getInt(temp2[1]), NumberUtils.getInt(temp2[2]));
			if (begin.compareTo(ends) < 0) {
				Map map = new HashMap();
				ends.add(Calendar.YEAR, -NumberUtils.getInt(temp[0]));
				ends.add(Calendar.MONTH, -NumberUtils.getInt(temp[1]));
				ends.add(Calendar.DATE, -NumberUtils.getInt(temp[2]));
				map.put("YEAR", ends.get(Calendar.YEAR));
				map.put("MONTH", ends.get(Calendar.MONTH) + 1);
				map.put("DAY", ends.get(Calendar.DATE));
				return map;
			}
		}
		return null;
	}

	public static Date rollYear(Date d, int year) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.YEAR, year);
		return cal.getTime();
	}

	public static Date rollDate(Date d, int year, int mon, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.YEAR, year);
		cal.add(Calendar.MONTH, mon);
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}

	// v1.6.6.2 RDPROJECT-277 xx 2013-10-22 start
	/**
	 * 获取当前时间-时间戳
	 * 
	 * @return
	 */
	public static int getNowTime() {
		return Integer.parseInt((System.currentTimeMillis() / 1000) + "");
	}
	// v1.6.6.2 RDPROJECT-277 xx 2013-10-22 end

	public static String getTimeStr(Date time) {
		long date = time.getTime();
		String str = Long.toString(date / 1000);
		return str;
	}

	public static String getTimeStr(String dateStr, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date;
		date = sdf.parse(dateStr);
		String str = DateUtils.getTimeStr(date);
		return str;
	}

	public static String rollMonth(String addtime, String time_limit) {
		Date t = DateUtils.rollDate(DateUtils.getDate(addtime), 0, NumberUtils.getInt(time_limit), 0);
		return t.getTime() / 1000 + "";
	}

	public static String rollDay(String addtime, String time_limit_day) {
		Date t = DateUtils.rollDate(DateUtils.getDate(addtime), 0, 0, NumberUtils.getInt(time_limit_day));
		return t.getTime() / 1000 + "";
	}

	// 获取本周日的日期
	public static String getCurrentWeekday() {
		int weeks = 0;
		int mondayPlus = DateUtils.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
		Date monday = currentDate.getTime();

		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获得当前日期与本周日相差的天数
	private static int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {
			return 0;
		} else {
			return 1 - dayOfWeek;
		}
	}

	// 获得本周一的日期
	public static String getMondayOFWeek() {
		int weeks = 0;
		int mondayPlus = DateUtils.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		Date monday = currentDate.getTime();

		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 获取当前月第一天：
	public static String getFirstDayOfMonth(String first) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		first = format.format(c.getTime());
		return first;
	}

	// 获取当月最后一天
	public static String getLastDayOfMonth(String last) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		last = format.format(ca.getTime());
		return last;
	}

	// v1.6.6.1 融华财富-首页显示统计 ljd 2013-10-15 start
	// 获取上个月第一天 00:00:00
	public static String getFirstDayOfLastMonth() {
		long time = 0;
		String monthBegin = getFirstDayOfMonth(null);
		try {
			Date lastMonthBegin = getDate(getTimeStr(monthBegin, "yyyy-MM-dd"));
			Date dateBegin = addMonth(lastMonthBegin, -1);
			time = dateBegin.getTime() / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return String.valueOf(time);
	}

	// 获取上个月最后一天 23:59:59
	public static String getLastDayOfLastMonth() {
		long time = 0;
		String monthEnd = getLastDayOfMonth(null);
		try {
			Date lastMonthEnd = getDate(getTimeStr(monthEnd, "yyyy-MM-dd"));
			Date dateEnd = addMonth(lastMonthEnd, -1);
			time = getLastSecIntegralTime(dateEnd).getTime() / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return String.valueOf(time);
	}
	// v1.6.6.1 融华财富-首页显示统计 ljd 2013-10-15 end

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		try {
			System.out.println(getTimeMillis("00:00:00"));
			
			System.out.println(getTimeMillis("23:59:59"));
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/*
		 * System.out.println( DateUtils.dateStr2("1328383257")); int
		 * a=1360030690-1328284800;
		 * System.out.println(DateUtils.getIntegralTime().getTime()/1000);
		 * System.out.println(DateUtils.getLastIntegralTime().getTime()/1000);
		 * Date nowDay=DateUtils.getLastSecIntegralTime(new Date()); Date
		 * aa=DateUtils.rollDay(nowDay, -1); Date bb=DateUtils.rollDay(nowDay,
		 * -7); System.out.println(aa); System.out.println(bb); String
		 * sundayString=DateUtils.getCurrentWeekday()+" 23:59:59"; String
		 * mondayString=DateUtils.getMondayOFWeek()+"  00:00:00";
		 * System.out.println(sundayString);
		 * System.out.println(DateUtils.getTime(sundayString)+"");
		 * System.out.println(DateUtils.getTime(mondayString)+"");
		 */

	}

	/**
	 * 获取 date 时间 00:00:00 000
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstSecIntegralTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取当前时间 00:00:00 000
	 * 
	 * @return
	 */
	public static Date getFirstSecIntegralTime() {

		return getFirstSecIntegralTime(new Date());
	}

	/**
	 * 获取 date 时间 23:59:59 000
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastSecIntegralTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(date.getTime());
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.SECOND, 51);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取当前时间 23:59:59 000
	 * 
	 * @return
	 */
	public static Date getLastSecIntegralTime() {

		return getLastSecIntegralTime(new Date());
	}

	/**
	 * 获取当前时间的秒数字符串
	 * 
	 * @return
	 */
	public static String getCurrentSecStr() {
		return String.valueOf(getCurrentSec());
	}

	/**
	 * 获取当前时间的秒数
	 * 
	 * @return
	 */
	public static long getCurrentSec() {
		return System.currentTimeMillis() / 1000;
	}

	/**
	 * 返回 date 增加 day 天数后的结果
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date addDay(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}


	 
	/**
	 * 返回 date 增加 month 月数后的结果
	 * 
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date addMonth(Date date, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, month);
		return cal.getTime();
	}

	/**
	 * 获取指定时间对应的毫秒数
	 * 
	 * @param time HH:mm:ss
	 * @return
	 */
	public static long getTimeMillis(String time) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(FORMAT_YYYY_MM_DD + Constants.STR_SPACE + FORMAT_HH_MM_SS);
			DateFormat dayFormat = new SimpleDateFormat(FORMAT_YYYY_MM_DD);
			String strTime = dayFormat.format(new Date()) +  Constants.STR_SPACE  + time;
			Date curDate = dateFormat.parse(strTime);
			return curDate.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public static String getDateFormatLong(Long date){
 		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 		return format1.format(date);
 	}

	/**
	 * 获取两个时间的时间差
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static String getDifferenceTime(Date beginDate,Date endDate){
		long l = endDate.getTime() - beginDate.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		String differenceTime = day + "天" + hour + "小时" + min + "分钟";
		return differenceTime;
	}

	/**
	 * 将时间戳转换为时间
	 * @param time
	 * @return
	 */
	public static String timeChangeToDate(Long time){
		String date1 = null;
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(null != time){
				long lt = new Long(time);
				Date date = new Date(lt);
				date1 = sdf.format(date);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return date1;
	}

	/**
	 * 时间类型为2018-08-30T08:32:13.000Z进行转换
	 * @param dateStr
	 * @return
	 */
	public static String dateStrChangeToTime(String dateStr){
		String date = null;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			if(StringUtils.isNotEmpty(dateStr)){
				Date date1 = df.parse(dateStr);
				date = df1.format(date1);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 给定开始和结束时间，遍历之间的所有日期
	 *
	 * @param startAt 开始时间，例：2017-04-04
	 * @param endAt   结束时间，例：2017-04-11
	 * @return 返回日期数组
	 */
	public static List<String> queryData(String startAt, String endAt) {
		List<String> dates = new ArrayList<>();
		try {
			Date startDate = dateFormat.parse(startAt);
			Date endDate = dateFormat.parse(endAt);
			dates.addAll(queryData(startDate, endDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dates;
	}

	/**
	 * 给定开始和结束时间，遍历之间的所有日期
	 *
	 * @param startAt 开始时间，例：2017-04-04
	 * @param endAt   结束时间，例：2017-04-11
	 * @return 返回日期数组
	 */
	public static List<String> queryData(Date startAt, Date endAt) {
		List<String> dates = new ArrayList<>();
		Calendar start = Calendar.getInstance();
		start.setTime(startAt);
		Calendar end = Calendar.getInstance();
		end.setTime(endAt);
		while (start.before(end) || start.equals(end)) {
			dates.add(dateFormat.format(start.getTime()));
			start.add(Calendar.DAY_OF_YEAR, 1);
		}
		return dates;
	}

 	
}
